/* @Parameters success [Function] 数据库连接成功回调
 * @Parameters error   [Function] 数据库连接失败回调 */
module.exports = function(success, error) {
    if (typeof error !== "function") {
        error = () => { console.log("数据库连接失败"); };
    } // 判断参数error是否有设置默认值
    const mongoose = require("mongoose");
    const { DBHOST, DBPORT, DBNAME } = require("./config");
    mongoose.set("strictQuery", true); // 启用严格模式(消除警告)
    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`);
    mongoose.connection.once("open", () => { success(); }); // 连接成功(单次)
    mongoose.connection.on("error", () => { error(); });    // 连接错误(失败)
    mongoose.connection.on("close", () => { console.log("数据库连接关闭"); });
};
